#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/2/7 10:59
# @Author  : Deyu Tian
# @Site    : 
# @File    : linearRegr.py
# @Software: PyCharm Community Edition

import matplotlib.pyplot as plt
import numpy as np
from sklearn import linear_model
from sklearn.metrics import mean_squared_error, r2_score
from sklearn import preprocessing
from image_tools import *
import config
import regr_config

def regr_with_nodata_mean():
    imggt, bands = readSixs(config.surf6S)
    cos_i = readIncidents(config.indicent)
    cos_i = cos_i.ravel().reshape(-1, 1)
    print("max and min of cos_i:", np.max(cos_i), np.min(cos_i))
    print("shape of cos_i:", cos_i.shape)
    cos_i[cos_i < -5] = np.nan
    miss = preprocessing.Imputer(missing_values='NaN', strategy='mean')
    miss.fit(cos_i)
    cos_i = miss.transform(cos_i)
    print("max and min of cos_i(missing processed):", np.max(cos_i), np.min(cos_i))
    print("shape of cos_i(missing processed):", cos_i.shape)
    print("samples of cos_i", cos_i.shape[0])
    for k in range(0, 6):
        bands[k] = bands[k].ravel().reshape(-1, 1)
        print("max and min of band:", np.max(bands[k]), np.min(bands[k]))
        print("shape of band:", bands[k].shape)
        bands[k][bands[k] < -0.05] = np.nan
        #print(bands[k][0, 0])
        miss = preprocessing.Imputer(missing_values='NaN', strategy='mean')
        miss.fit(bands[k])
        bands[k] = miss.transform(bands[k])
        print("max and min of band(missing processed):", np.max(bands[k]), np.min(bands[k]))
        print("shape of band(missing processed):", bands[k].shape)
        print("samples of band data:", bands[k].shape[0])
        x_train = cos_i[:7534224]
        print(x_train.shape)
        x_test = cos_i[7534224:]
        print(x_test.shape)
        y_train = bands[k][:7534224]
        print(y_train.shape)
        y_test = bands[k][7534224:]
        print(y_test.shape)
        print(LinearRegr(x_train, y_train, x_test, y_test))

def regr_with_clip():
    imggt, bands = readSixs(regr_config.Sixs_regr)
    cos_i = readIncidents(regr_config.incident_regr)
    cos_i = cos_i.ravel().reshape(-1, 1)
    print("max and min of cos_i:", np.max(cos_i), np.min(cos_i))
    print("shape of cos_i:", cos_i.shape)
    print("max and min of cos_i(missing processed):", np.max(cos_i), np.min(cos_i))
    print("shape of cos_i(missing processed):", cos_i.shape)
    print("samples of cos_i", cos_i.shape[0])
    for k in range(0, 6):
        bands[k] = bands[k].ravel().reshape(-1, 1)
        print("max and min of band:", np.max(bands[k]), np.min(bands[k]))
        print("shape of band:", bands[k].shape)
        print("max and min of band(missing processed):", np.max(bands[k]), np.min(bands[k]))
        print("shape of band(missing processed):", bands[k].shape)
        print("samples of band data:", bands[k].shape[0])
        x_train = cos_i[:534224]
        print(x_train.shape)
        x_test = cos_i[534224:]
        print(x_test.shape)
        y_train = bands[k][:534224]
        print(y_train.shape)
        y_test = bands[k][534224:]
        print(y_test.shape)
        print(LinearRegr(x_train, y_train, x_test, y_test))


def LinearRegr(x_train, y_train, x_test, y_test):
    #create linear regression object
    regr = linear_model.LinearRegression()
    #train the model using the trainset
    regr.fit(x_train, y_train)
    #make predictions using the testing set
    y_pred = regr.predict(x_test)
    #compute the metrics
    MSE = mean_squared_error(y_test, y_pred)
    r2 = r2_score(y_test, y_pred)
    coef = regr.coef_
    inter = regr.intercept_
    estimator_out = [MSE, r2, coef, inter]
    #plot outputs
    # plt.scatter(x_test, y_test)
    # plt.plot(x_test, y_pred)
    # plt.xticks(())
    # plt.yticks(())
    # plt.show()
    return estimator_out



if __name__ == '__main__':
    regr_with_nodata_mean()